package com.ssy.lingxi.search.controller.mobile;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.search.api.model.request.PurchaseBatchRequest;
import com.ssy.lingxi.search.api.model.request.PurchaseRequest;
import com.ssy.lingxi.search.api.model.request.SimpleIdListRequest;
import com.ssy.lingxi.search.entity.Purchase;
import com.ssy.lingxi.search.model.response.PurchaseResponse;
import com.ssy.lingxi.search.service.IPurchaseService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @menu: APP-商城--进货单管理
 * 进货单管理类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/9/4
 */
@RestController
@RequestMapping("/search/mobile/shop/purchase")
public class MobilePurchaseController extends BaseController {

    @Resource
    private IPurchaseService purchaseService;

    /**
     * 查询进货单列表
     */
    @RequestMapping(value = "/getPurchaseList", method = RequestMethod.GET)
    public Wrapper<List<PurchaseResponse>> getPurchaseList() {
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(purchaseService.getPurchaseList(sysUser, shopId, true, null, null, null));
    }

    /**
     * 新增/修改进货单
     * @param purchaseRequest 进货单实体
     * @return 进货单数量
     */
    @RequestMapping(value = "/saveOrUpdatePurchase", method = RequestMethod.POST)
    public Wrapper<Integer> saveOrUpdatePurchase(@RequestBody PurchaseRequest purchaseRequest){
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        Purchase purchase = this.modelMapper.map(purchaseRequest, Purchase.class);
        purchase.setShopId(shopId);
        return Wrapper.success(purchaseService.saveOrUpdatePurchase(sysUser, purchase));
    }

    /**
     * 批量新增进货单
     * @param purchaseBatchRequest 进货单实体
     * @return 是否成功
     */
    @RequestMapping(value = "/savePurchaseBatch", method = RequestMethod.POST)
    public Wrapper<Boolean> savePurchaseBatch(@RequestBody PurchaseBatchRequest purchaseBatchRequest){
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        List<PurchaseRequest> purchaseList = purchaseBatchRequest.getPurchaseBatchList();
        return Wrapper.success(purchaseService.savePurchaseBatch(sysUser, shopId, purchaseList));
    }

    /**
     * 删除进货单
     * @param simpleIdListRequest
     * @return
     */
    @RequestMapping(value = "/deletePurchase", method = RequestMethod.POST)
    public Wrapper<String> deletePurchase(@RequestBody SimpleIdListRequest simpleIdListRequest){
        return Wrapper.success(purchaseService.deletePurchase(simpleIdListRequest));
    }

    /**
     * 查询进货单数量
     */
    @RequestMapping(value = "/getPurchaseCount", method = RequestMethod.GET)
    public Wrapper<Integer> getPurchaseCount(){
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(purchaseService.getPurchaseCount(sysUser, shopId, null, null, null));
    }

}
